package com.os.crm.controller;

import com.os.crm.pojo.TaskResult;
import com.os.crm.service.TaskResultService;
import com.os.util.Tools;
import com.os.util.page.PageRequest;
import com.os.util.page.PageResult;
import entity.Param;
import entity.Result;
import entity.StatusCode;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/taskResult")
@RefreshScope
@Slf4j
public class TaskResultController {
    @Autowired
    private TaskResultService taskResultService;
    
    @ApiOperation(value = "分页查询任务执行结果列表信息" , notes = "分页查询任务执行结果列表信息")
    @RequestMapping(value="/selectByPage", method=RequestMethod.GET)
    public Result selectByPage(@RequestParam(value="page",required = false) Integer page,
                               @RequestParam(value="pageSize",required = false) Integer pageSize
                              ,@RequestParam(value="task_name",required = false) String task_name
                               ){
        Map map = new HashMap();
        List paramList = new ArrayList();
        if(task_name!=null){
            paramList.add(new Param("task_name", task_name + "", "String","like"));
        }
        map.put("condstr", Tools.genCondStr("", paramList));
        PageRequest pageRequest = new PageRequest();
        pageRequest.setPageNum(page);
        pageRequest.setPageSize(pageSize);
        PageResult pageResult = taskResultService.selectByPage(pageRequest,map);
        Result result = new Result(true, StatusCode.OK,"查询成功",pageResult.getContent());
        result.setTotalCount(pageResult.getTotalSize());
        return result;
    }
    @ApiOperation(value = "按照ID查询任务执行结果信息" , notes = "按照ID查询任务执行结果信息")
    @ApiImplicitParam(name = "task_result_id",value = "ID",required = true,dataType = "int")
    @RequestMapping(value="/selectById/{task_result_id}", method=RequestMethod.GET)
    public Result selectById(@PathVariable int task_result_id){
        return new Result(true, StatusCode.OK,"查询成功", taskResultService.selectById(task_result_id));
    }
    @ApiOperation(value = "新增任务执行结果信息" , notes = "新增任务执行结果信息")
    @RequestMapping(value="/add",method = RequestMethod.POST)
    public Result insert(@RequestBody TaskResult taskResult){
        taskResultService.insert(taskResult);
        return new Result(true, StatusCode.OK,"添加成功");
    }
    @ApiOperation(value = "更新任务执行结果信息" , notes = "更新任务执行结果信息")
    @RequestMapping(value="/update",method = RequestMethod.POST)
    public Result update(@RequestBody TaskResult taskResult){
        taskResultService.update(taskResult);
        return new Result(true, StatusCode.OK,"修改成功", taskResult);
    }
    @ApiOperation(value = "删除任务执行结果信息" , notes = "删除任务执行结果信息")
    @RequestMapping(value="/{task_result_id}",method = RequestMethod.DELETE)
    public Result delete(@PathVariable int task_result_id){
        taskResultService.deleteById(task_result_id);
        return new Result(true, StatusCode.OK,"删除成功");
    }
}